/**
 * 
 */
package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.SubmitRecipeBean;
import bean.UserBean;
import java.util.HashSet;
import java.util.Set;

import project.taip.wawwe.db.Services;
import project.taip.wawwe.model.Ingredient;
import project.taip.wawwe.model.RateBean;
import project.taip.wawwe.model.Recipe;
import org.hibernate.Session;
import util.RecipeUtil;

/**
 * @author Andrei Iordache
 *
 */
public class SubmitRecipeServlet extends HttpServlet implements RecipeServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public SubmitRecipeServlet() {
		super();
	}

	public void doGet(HttpServletRequest request,
			HttpServletResponse response) {
		processPostGetRequest(request, response);
	}
	
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) {
		processPostGetRequest(request, response);
	}
	
	/* (non-Javadoc)
	 * @see servlet.RecipeServlet#processPostGetRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void processPostGetRequest(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		SubmitRecipeBean submitRecipeBean = storeRecipe(request);
//		getServletContext().log("blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		try {
			getServletContext().getRequestDispatcher("/submit_result.jsp?process=false").
						forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			
//			e.printStackTrace();
//			getServletContext().log(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
//			getServletContext().log(e.getMessage());
		}
	}

	private SubmitRecipeBean storeRecipe(HttpServletRequest request) {
		SubmitRecipeBean submitRecipeBean = null;
		try {
			submitRecipeBean = (SubmitRecipeBean) request
					.getAttribute("instance");
			
			UserBean user = (UserBean) request.getAttribute("user");
			project.taip.wawwe.model.UserBean user2 = new project.taip.wawwe.model.UserBean();
			user2.setAge(user.getAge());
			user2.setCountry(user.getCountry());
			user2.setSex(user.getSex());
			Recipe recipe = new Recipe();
			recipe.setDescription(submitRecipeBean.getDescription());
			recipe.setName(submitRecipeBean.getRecipeName());
			recipe.setOrigin(submitRecipeBean.getOrigin());
			ArrayList<Ingredient> array = RecipeUtil.getIngredientsFromText(recipe.getDescription());
                        Set<Ingredient> set = new HashSet<Ingredient>();
                        for (Ingredient i : array) { 
                            set.add(i);
                        }
                        recipe.setIngredients(set);
//			String[] ingredients = submitRecipeBean.getIngredients()
//					.split(",");

//		 for (String s : ingredients) {
//                        String[] ing = s.split(" ");
//                        Ingredient i = new Ingredient();
//                        switch (ing.length) {
//                            case 3:
//                                i.setQuantity(Integer.parseInt(ing[0]));
//                                i.setUnit(ing[1]);
//                                i.setName(ing[2]);
//                                break;
//                            case 2:
//                                i.setName(ing[1]);
//                                i.setQuantity(Integer.parseInt(ing[0]));
//                                break;
//                            case 1:
//                                i.setName(ing[0]);
//                                break;
//
//                        }
//                        recipe.addIngredients(i);
//                    }
			Services.submitRecipe(recipe, user2);
		} catch (Exception e) {
//			getServletContext().log(e.getMessage());
		}
		return submitRecipeBean;
	}

}
